//
//  M_ProductCell.swift
//  letuofu
//
//  Created by WH-JS on 2019/12/16.
//  Copyright © 2019 WH-JS. All rights reserved.
//

import UIKit

class M_ProductCell: QuickTableCellWithInit<[String : Any?]> {
    //定义一个闭包类型
    typealias addBlock = ([String : Any?]) -> Void
    //声明一个变量
    var addBtnBack: addBlock?
    //定义一个方法,方法的参数为和swiftBlock类型一致的闭包,并赋值给callBack
    func callAddBlock(block: @escaping addBlock) {
        addBtnBack = block
    }
    
    let productImg = UIImageView()
    let priceImg = UIImageView()
    let addBtn = Button()
    let titleLab = UILabel()
    let priceLab = UILabel()
    let subLab = UILabel()
    let jdLab = UILabel()
    
    override class func heightOf(_ data:[String:Any?])->CGFloat{
        return 138
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override func initCell(container: UIView) {
        container.backgroundColor = .white
        let bgView = drawView() { (make) in
            make.top.equalToSuperview().offset(10)
            make.left.equalToSuperview().offset(10)
            make.width.equalTo(118)
            make.height.equalTo(118)
        }
        
        bgView.drawImage(imageView: productImg,named: nil) { (make) in
            make.width.equalToSuperview().offset(-12)
            make.height.equalToSuperview().offset(-12)
            make.center.equalToSuperview()
        }
        // 设置边框
        productImg.layer.borderWidth = 1
        productImg.layer.borderColor = UIColor.xF2F2F2.cgColor
        productImg.layer.cornerRadius = 4
        productImg.clipsToBounds = true
        
        productImg.drawLabel(label: jdLab,text: "京东配送", fontSize: 9, textColor: .white) { (make) in
            make.top.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(16)
            make.width.equalTo(51)
        }
//        jdLab.layer.cornerRadius = 5
        jdLab.font = .boldSystemFont(ofSize: 9.5)
        jdLab.backgroundColor = .xFC4C02
        jdLab.textAlignment = .center
        jdLab.clipsToBounds = true  
        
        drawLabel(label: titleLab, text: "", fontSize: 14, fontWeight: .medium, textColor: .x333333) { (make) in
            make.left.equalTo(bgView.snp.right).offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalToSuperview().offset(10)
        }
        titleLab.numberOfLines = 2
        
        //画横线
        let line = drawView { (make) in
            make.left.equalToSuperview().offset(140.5)
            make.right.equalToSuperview().offset(10)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        line.backgroundColor = .xF2F2F2
        drawLabel(label: subLab, text: "", fontSize: 13, fontWeight: .regular, textColor: .x999999) { (make) in
            make.left.equalTo(bgView.snp.right).offset(10)
            make.bottom.equalToSuperview().offset(-10)
        }
        drawImage(imageView: priceImg,named: "jifen_13") { (make) in
            make.left.equalTo(bgView.snp.right).offset(10)
            make.bottom.equalToSuperview().offset(-29.5)
        }
        drawLabel(label: priceLab, text: "", fontSize: 18, fontWeight: .regular, textColor: .xFC4C02) { (make) in
            make.left.equalTo(priceImg.snp.right).offset(6)
            make.bottom.equalToSuperview().offset(-25)
        }
        drawButton(button: addBtn,imageNamed: "gouwuche_17") { (make) in
            make.right.equalToSuperview().offset(-10.5)
            make.bottom.equalToSuperview().offset(-11)
        }
        addBtn.hitEdgInsets = .init(top: -20, left: -20, bottom: -20, right: -20)
        addBtn.addTarget(self, action: #selector(_addButtonClick(button:)), for: .touchUpInside)
    }
    
    override func fillCell(_ data: [String:Any?], at index: Int) {
        if var urlstring = data.getString("listPicURL") {
            if data.getString("zoneCode") == "105002" {
                urlstring = (JDIMG_HOST_URL+urlstring).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
                subLab.isHidden = false
                jdLab.isHidden = false
            }
            else {
                urlstring = (IMG_HOST_URL+urlstring).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
                subLab.isHidden = true
                jdLab.isHidden = true
            }
            let url = URL(string: urlstring)
            productImg.sd_setImage(with:url)
        }else{
            productImg.image = nil
        }
        titleLab.text = data.getString("productName")
        subLab.text = "市场价 " + anyToString(data["marketCentPricing"] ?? "")! + (data.getString("centUnitName") ?? "")
        priceLab.text = anyToString(data["employeeCentPrice"] ?? "")
    }
    
    @objc func _addButtonClick(button: Button) {
        if addBtnBack != nil {
            addBtnBack!(self.data ?? [:])
        }
    }
}

class M_ProductColCell: UICollectionViewCell {
    //定义一个闭包类型
    typealias addBlock = () -> Void
    //声明一个变量
    var addBtnBack: addBlock?
    //定义一个方法,方法的参数为和swiftBlock类型一致的闭包,并赋值给callBack
    func callAddBlock(block: @escaping addBlock) {
        addBtnBack = block
    }
    
    let width = 375//获取屏幕宽
    let productImg = UIImageView()
    let priceImg = UIImageView()
    let addBtn = Button()
    let titleLab = UILabel()
    let priceLab = UILabel()
    let subLab = UILabel()
    let jdLab = UILabel()
    var celldata : [String : Any?]? {
        didSet{
            loadData()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initView(){
        self.backgroundColor = .white
        self.cornerRadius(4)
        
        self.drawImage(imageView: productImg,named: nil) { (make) in
            make.top.equalToSuperview().offset(12.5)
            make.width.equalToSuperview().offset(-25)
            make.height.equalToSuperview().offset(-91)
            make.centerX.equalToSuperview()
        }
        
        self.drawLabel(label: jdLab,text: "京东配送", fontSize: 9, textColor: .white) { (make) in
            make.top.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(16)
            make.width.equalTo(51)
        }
        jdLab.font = .boldSystemFont(ofSize: 9.5)
        jdLab.backgroundColor = .xFC4C02
        jdLab.textAlignment = .center
        self.clipsToBounds = true
        
        drawLabel(label: titleLab, text: "", fontSize: 12, fontWeight: .medium, textColor: .x333333) { (make) in
            make.left.equalToSuperview().offset(8)
            make.right.equalToSuperview().offset(-8)
//            make.bottom.equalToSuperview().offset(-57)
            make.top.equalTo(productImg.snp.bottom).offset(8)
            make.height.equalTo(29)
        }
        titleLab.numberOfLines = 2
        
        drawImage(imageView: priceImg,named: "jifen_13") { (make) in
            make.left.equalToSuperview().offset(8)
            make.bottom.equalToSuperview().offset(-23-3)
        }
        drawLabel(label: priceLab, text: "", fontSize: 15, fontWeight: .regular, textColor: .xFC4C02) { (make) in
            make.left.equalTo(priceImg.snp.right).offset(8)
            make.bottom.equalToSuperview().offset(-23)
        }
        
        drawLabel(label: subLab, text: "", fontSize: 11, fontWeight: .regular, textColor: .x999999) { (make) in
            make.left.equalToSuperview().offset(8)
            make.bottom.equalToSuperview().offset(-7)
        }
        
        drawButton(button: addBtn,imageNamed: "gouwuche_17") { (make) in
            make.right.equalToSuperview().offset(-10)
//            make.centerY.equalTo(priceImg)
            make.bottom.equalTo(subLab)
        }
        addBtn.hitEdgInsets = .init(top: -20, left: -20, bottom: -20, right: -20)
        addBtn.addTarget(self, action: #selector(_addButtonClick(button:)), for: .touchUpInside)
    }
    
    func loadData() {
        if var urlstring = celldata?.getString("listPicURL") {
            if celldata?.getString("zoneCode") == "105002" {
                urlstring = (JDIMG_HOST_URL+urlstring).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
                subLab.isHidden = false
                jdLab.isHidden = false
            }
            else {
                urlstring = (IMG_HOST_URL+urlstring).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
                jdLab.isHidden = true
                subLab.isHidden = true
                priceImg.snp_remakeConstraints({ (make) in
                    make.left.equalToSuperview().offset(8)
                    make.bottom.equalToSuperview().offset(-7-3)
                })
                priceLab.snp_remakeConstraints({ (make) in
                    make.left.equalTo(priceImg.snp.right).offset(8)
                    make.bottom.equalToSuperview().offset(-7)
                })
            }
            let url = URL(string: urlstring)
            productImg.sd_setImage(with:url)
        }else{
            productImg.image = nil
        }
        titleLab.text = celldata?.getString("productName")
        subLab.text = "市场价 " + anyToString(celldata?["marketCentPricing"] ?? "")! + (celldata?.getString("centUnitName") ?? "")
        priceLab.text =  anyToString(celldata?["employeeCentPrice"] ?? "")
    }
    
    @objc func _addButtonClick(button: Button) {
        if addBtnBack != nil {
            addBtnBack!()
        }
    }
}
